//
//  Problem.m
//  nova
//
//  Created by user on 11/06/08.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Problem.h"


@implementation Problem
@synthesize problemsObject;
@synthesize commentList;
@synthesize resourceList;
@synthesize historyList;
@synthesize isNew;
@synthesize changed;
@synthesize problemState;

- (id) init{
	self = [super init];
	commentList = [[NSMutableArray alloc] init];
	resourceList = [[NSMutableArray alloc] init];
	problemsObject = [[Problems alloc] init];
	isNew = YES;
	changed = YES;
	problemState = nil;
	[problemsObject setVDateTime_Created : [Library getCurrentTime]];
	[problemsObject setVDescription:@"Add a description here..."];
	return self;
}

- (id) initWithProblems : (Problems *) problems {
	[self init];
	isNew = NO;
	changed = NO;
	problemsObject	= problems;
	[self loadResources];
	[self loadState];
	return self;
}

- (id) initWithProject : (Project *) project{
	[self init];
	[problemsObject setVId_Projects:[project getProjectId]];
	return self;
}

- (NSString *) getProblemDescription{
	return [problemsObject vDescription];
}

- (NSString *) getResponseOwner{
	//return [[resourceList objectAtIndex:0] getName];
	return @"YOU";
}

- (NSString *) getProblemState{
	return @"EstadoEstatico";
}

- (NSString *) getInsertString{
	return [problemsObject getInsertStringForSelf];
}

- (NSString *) getUpdateString{
	return [problemsObject getUpdateStringForSelf];
}


- (NSString *) getImpact_Description{
	return [problemsObject vImpact_Description];
}

- (NSString *) getResponse_Description{
	return [problemsObject vResponse_Description];
}

- (double) getResponse_Finish{
	return [problemsObject vResponse_Finish];
}

- (double) getDateTimeCreated{
	return [problemsObject vDateTime_Created];
}

- (int) getProblemId{
	return [problemsObject vId_Problems];
}

- (SEL) getSetResponseFinishSelector{
	return @selector (setResponse_Finish:);
}

- (void) setResponse_Finish : (double) responseFinishDate{
	[problemsObject setVResponse_Finish:responseFinishDate];
}

- (void) setProblemDescription : (NSString *) desc{
	changed = YES;
	[problemsObject setVDescription:desc];
}

- (void) setImpact_Description : (NSString *) ipm{
	changed = YES;
	[problemsObject setVImpact_Description:ipm];
}

- (void) setResponse_Description :  (NSString *) resp{
	changed = YES;
	[problemsObject setVResponse_Description :  resp];
}

- (void) loadComments{
	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayComments:[problemsObject getSelectStringForComments] : [problemsObject colComments]];
	[context dealloc];
	for(Comments *p in [problemsObject colComments])
	{
		Comment *comment = [[Comment alloc] initWithComments:p];
		[resourceList addObject:comment];
	}
	
}

- (void) loadResources{
	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayResources:[problemsObject getSelectStringForResources] : [problemsObject colProblemsResources]];
	[context dealloc];
	for(Resources *p in [problemsObject colProblemsResources])
	{
		Resource *resource = [[Resource alloc] initWithResources:p];
		[resourceList addObject:resource];
	}
}

- (void) loadHistory{
	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayHistorys:[problemsObject getSelectStringForHistorys] : [problemsObject colHistorys]];
	[context dealloc];
	for(Historys *p in [problemsObject colHistorys])
	{
		History *history = [[History alloc] initWithHistorys:p];
		[historyList addObject:history];
	}
}

- (void) loadState {
	DataContext *context = [[DataContext alloc]init];
	NSMutableArray * colStates = [[NSMutableArray alloc] init];
	[context getFromQueryToArrayStates: [problemsObject getSelectStringForStates] : colStates];
	[context dealloc];
	if([colStates count]>0){
		problemState = [[State alloc] initWithStates:[colStates objectAtIndex:0]];
		[colStates removeAllObjects];
	}else {
		problemState = nil;
	}
	[colStates dealloc];
}


- (void) unloadComments{/*
						 int conta = [actionList count];
						 for(int i = 0; i < conta; i++)
						 {
						 Action * action = [actionList objectAtIndex: 0];
						 [actionList removeObjectAtIndex:0];
						 
						 //Clean the array of objects
						 [[projects colActions] removeObjectAtIndex:0];
						 
						 
						 
						 [action dealloc];
						 action = nil;
						 }*/
}

- (void) unloadResources{/*
						  int conta = [actionList count];
						  for(int i = 0; i < conta; i++)
						  {
						  Action * action = [actionList objectAtIndex: 0];
						  [actionList removeObjectAtIndex:0];
						  
						  //Clean the array of objects
						  [[projects colActions] removeObjectAtIndex:0];
						  
						  
						  
						  [action dealloc];
						  action = nil;
						  }*/
}


- (void) unloadHistory{/*
						int conta = [actionList count];
						for(int i = 0; i < conta; i++)
						{
						Action * action = [actionList objectAtIndex: 0];
						[actionList removeObjectAtIndex:0];
						
						//Clean the array of objects
						[[projects colActions] removeObjectAtIndex:0];
						
						
						
						[action dealloc];
						action = nil;
						}*/
}


- (void) dealloc{
    [problemsObject dealloc];
	problemsObject = nil;
	[super dealloc];
	//Alterar aqui para remover arrays
	/*
	 @property (readonly)  NSMutableArray *commentList;
	 @property (readonly)  NSMutableArray *resourceList;
	 @property (readonly)  NSMutableArray *historyList;
	 */
}
@end
